package com.zhz.common.tool.sms.enums;

import com.zhz.common.tool.sms.SmsSender;
import com.zhz.common.tool.sms.aliyun.AliyunSmsSender;
import com.zhz.common.tool.sms.boshitong.BoshitongSmsSender;
import com.zhz.common.tool.sms.properties.SmsProperties;
import lombok.AllArgsConstructor;

import java.util.Objects;

/**
 * sms发送类型
 *
 * @author zhouhengzhe
 * @version 1.0
 * @date 2023/4/24 11:26
 */
@AllArgsConstructor
public enum SmsSendTypeEnum implements  SmsSendType{
    ALIYUN("aliyun", "阿里云"){
        /**
         * 创建短信发送实例
         *
         * @param smsProperties 短信配置
         * @return 短信发送实例
         */
        @Override
        public SmsSender create(SmsProperties smsProperties) {
            SmsProperties.AliyunSmsProperties aliyun = smsProperties.getAliyun();
            if (Objects.isNull(aliyun)) {
                throw new RuntimeException("阿里云短信配置不能为空");
            }
            Objects.requireNonNull(aliyun.getAccessKeyId());
            Objects.requireNonNull(aliyun.getAccessKeySecret());
            Objects.requireNonNull(aliyun.getEndpoint());
            return new AliyunSmsSender(aliyun.getAccessKeyId(), aliyun.getAccessKeySecret(), aliyun.getEndpoint());
        }
    },
    BO_SHI_TONG("boshitong", "博士通") {
        /**
         * 创建短信发送实例
         *
         * @param smsProperties 短信配置
         * @return 短信发送实例
         */
        @Override
        public SmsSender create(SmsProperties smsProperties) {
            SmsProperties.Boshitong boshitong = smsProperties.getBoshitong();
            if (Objects.isNull(boshitong)) {
                throw new RuntimeException("博士通短信配置不能为空");
            }
            Objects.requireNonNull(boshitong.getUid());
            Objects.requireNonNull(boshitong.getPwd());
            Objects.requireNonNull(boshitong.getSmsUrl());
            Objects.requireNonNull(boshitong.getSrcPhone());
            return new BoshitongSmsSender(boshitong);
        }
    };

    private final String code;
    private final String message;

    /**
     * 异常值
     *
     * @return 异常值
     */
    public String getCode() {
        return this.code;
    }

    /**
     * 异常信息
     *
     * @return 异常信息
     */
    public String getMessage() {
        return this.message;
    }

    /**
     * 根据code获取枚举
     */
    public static SmsSendTypeEnum getByCode(String code) {
        for (SmsSendTypeEnum smsSendTypeEnum : SmsSendTypeEnum.values()) {
            if (smsSendTypeEnum.getCode().equals(code)) {
                return smsSendTypeEnum;
            }
        }
        return null;
    }
}
